#!/bin/sh
#
# Start/stop/restart the carbon-cache daemon
# Written by Mario Preksavec <mario@slackware.hr>

BIN_FILE=/usr/bin/carbon-cache
PID_FILE=/var/run/carbon-cache.pid
CONF_FILE=/etc/carbon/carbon.conf

rc_start() {
  if [ -x $BIN_FILE ] && [ -f $CONF_FILE ]; then
    if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2>/dev/null; then
      echo "carbon-cache daemon already running"
    else
      echo "Starting carbon-cache daemon:  $BIN"
      $BIN_FILE --config=$CONF_FILE --pidfile=$PID_FILE start
    fi
  fi
}

rc_stop() {
  if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2>/dev/null; then
    echo "Stopping carbon-cache daemon:  $BIN"
    $BIN_FILE --config=$CONF_FILE --pidfile=$PID_FILE stop \
      && rm -f $PID_FILE
  fi
}

rc_status() {
  if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2>/dev/null; then
    $BIN_FILE --config=$CONF_FILE --pidfile=$PID_FILE status
  fi
}

rc_restart() {
  rc_stop
  sleep 1
  rc_start
}

case "$1" in
  'start')
    rc_start
  ;;
  'stop')
    rc_stop
  ;;
  'restart')
    rc_restart
  ;;
  'status')
    rc_status
  ;;
  *)
    echo "usage $0 start|stop|restart|status"
  ;;
esac
